package com.carbeauty.logic;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

public class ImageComparator {
	private static ImageComparator loader = new ImageComparator();
	public static ImageComparator getInstance(){
		return loader;
	}
	private boolean isloaded= false;
	HashMap<String,ImageCompareResult> filedatamap ;
	private ImageComparator(){}
	
	/**
	 * 将一个目录下的所有图片都加载到内存中
	 * @throws IOException 
	 */
	public synchronized void   loadDiskFile() throws IOException{
		File file = new File(Constants.FILE_UPLOAD_DIR);
		if(!file.exists())
		{
			file.createNewFile();
		}
		if(file.isDirectory())
		{
			File[] files = file.listFiles();
			if(files!=null && files.length>0)
			{
				this.filedatamap = ImageFingerLoader.getInstance().load(files);
				Set<String> kes = this.filedatamap.keySet();
				Iterator<String> itkey = kes.iterator();
				while(itkey.hasNext())
				{
					String filePath = itkey.next();
					ImageCompareResult fp = this.filedatamap.get(filePath);
				}
			}
		}
		isloaded = true;
	}
	
	public ArrayList<ImageCompareResult> compareImage(String file)
	{
		if(!isloaded)
		{
			try {
				this.loadDiskFile();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
		ImageCompareResult srcFingerPrint =  SimilarImageSearch.produceFingerPrint(new File(file));
		Set<String> kes = this.filedatamap.keySet();
		Iterator<String> itkey = kes.iterator();
		ArrayList<ImageCompareResult> coms = new ArrayList<ImageCompareResult>();
		while(itkey.hasNext())
		{
			String filePath = itkey.next();
			ImageCompareResult fp = this.filedatamap.get(filePath);
			int R = fp.getColor().getR();
			int G = fp.getColor().getG();
			int B= fp.getColor().getB();
			int R2 = srcFingerPrint.getColor().getR();
			int G2 = srcFingerPrint.getColor().getG();
			int B2= srcFingerPrint.getColor().getB();
			int diff =Math.abs((R-R2))+Math.abs((G-G2))+Math.abs((B-B2));
			
			System.out.println(filePath+"=="+toHexEncoding(R,G,B));
			fp.setHanmingDistance(diff);
			coms.add(fp);
		}
		Collections.sort(coms,new Comparator<ImageCompareResult>(){
			public int compare(ImageCompareResult o1, ImageCompareResult o2) {
				
				int R = o1.getColor().getR();
				int G = o1.getColor().getG();
				int B= o1.getColor().getB();
				int R2 = o2.getColor().getR();
				int G2 = o2.getColor().getG();
				int B2= o2.getColor().getB();
						//System.out.println(o1.getFile()+"-R="+R+" G="+G+" B="+B+"  "+o2.getFile()+":"+"R2="+R2+" G2="+G2+" B2="+B2);
						
						
				int diff = R-R2+G-G2+B-B2;
//				return  diff>40?-1:1;
				return o1.getHanmingDistance()-o2.getHanmingDistance();
				
				
					
//			return 	o1.getColor().getR()+o1.getColor().getG()+o1.getColor().getB()
//				-o2.getColor().getR()-o2.getColor().getG()-o2.getColor().getB();
				 
				
			}
		});
		return coms;
	}
	
	
	//Color转换为16进制显示
    public static String toHexEncoding(int pr ,int pg,int pb) {
        String R, G, B;
        StringBuffer sb = new StringBuffer();
        R = Integer.toHexString(pr);
        G = Integer.toHexString(pg);
        B = Integer.toHexString(pb);
        R = R.length() == 1 ? "0" + R : R;
        G = G.length() == 1 ? "0" + G : G;
        B = B.length() == 1 ? "0" + B : B;
        sb.append("0x");
        sb.append(R);
        sb.append(G);
        sb.append(B);

        return sb.toString();
    }
	
	public static void main(String[] args)
	{
		try {
			ImageComparator.getInstance().loadDiskFile();
		} catch (IOException e) {
			e.printStackTrace();
		}
		ArrayList<ImageCompareResult> rss= ImageComparator.getInstance().compareImage(Constants.FILE_UPLOAD_DIR+"/41.jpg");
		for(ImageCompareResult rs:rss)
		{
			String filePath = rs.getFile();
			System.out.println(filePath);
		}
	}
	
}
